package com.yang.huawei.service.impl;

import com.alibaba.fastjson.JSON;
import com.yang.huawei.bean.MessageBean;
import com.yang.huawei.bean.UserActiveBean;
import com.yang.huawei.service.HuaweiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class HuaweiServiceImpl implements HuaweiService {
    /**
     * 原始的\r\n换行符
     */
    final static String _R_N = "\r\n";

    /**
     * 原始的\n换行符
     */
    final static String _N = "\n";

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    @Override
    public void produceMessage() {
        final String topic = "user_active";

//        List<MessageBean> messageBeanList = this.getMessageBeanList();
//        for(MessageBean messageBean : messageBeanList) {
//            kafkaTemplate.send(topic, JSON.toJSONString(messageBean));
//        }


        List<UserActiveBean> userActiveBeanList = this.getUserActiveBeanList();
        for(UserActiveBean userActiveBean : userActiveBeanList) {
            kafkaTemplate.send(topic, JSON.toJSONString(userActiveBean));
        }

    }

    private List<MessageBean> getMessageBeanList() {
        List<MessageBean> messageBeanList = new ArrayList<>();
        MessageBean messageBean = new MessageBean();
        messageBean.setSystemId("SYS_III000163");
        messageBean.setTopic("user_active");
        messageBean.setTime(1597554162456L);
        String content = "[{\"bizType\" : null, \"business\" :  null, \"content\" :  \"这是\n一个有故事的\r\n中国人。\"}]";
        messageBean.setContent(content);
        messageBeanList.add(messageBean);
        return messageBeanList;
    }


    /**
     * 编造向Kafka写入的数据
     * @return
     */
    private List<UserActiveBean> getUserActiveBeanList() {
        List<UserActiveBean> userActiveBeanList = new ArrayList<>();
        //第一条数据
        UserActiveBean userActiveBean = new UserActiveBean();
        userActiveBean.setId(UUID.randomUUID().toString());
        userActiveBean.setBizType("b1");
        userActiveBean.setCardNo("cardNo1");
        userActiveBean.setContent("这是一个有故事的中国人。" + _R_N + "从前有座山，山里有个庙，庙里有个老和尚。" + _N + "老和尚再给小和尚讲故事。");
        userActiveBean.setDestIp("192.168.1.8");
        userActiveBean.setDestPort(8082);
        userActiveBean.setErrorCode(null);
        userActiveBean.setErrorLog(null);
        userActiveBean.setFormatParam("personId=1");
        userActiveBean.setKeyWords("故事");
        userActiveBean.setLogType("file");
        userActiveBean.setModule("chinese people");
        userActiveBean.setNetworkCode("3");
        userActiveBean.setNetworkName("公安网");
        userActiveBean.setParams("personId=1");
        userActiveBean.setPoliceId("police1");
        userActiveBean.setRequestId(UUID.randomUUID().toString());
        userActiveBean.setResponse("ok");
        userActiveBean.setResponseTime(100);
        userActiveBean.setResponseType("200");
        userActiveBean.setSessionId(UUID.randomUUID().toString());
        userActiveBean.setSn("sn1234");
        userActiveBean.setSource("12.108.5.7");
        userActiveBean.setSourcePort(8033);
        userActiveBean.setSystemId("jzgk");
        userActiveBean.setSystemName("集中管控");
        userActiveBean.setTerminalIp("10.5.4.3");
        userActiveBean.setTime(1605861092000l);
        userActiveBean.setUri("/person/info");
        userActiveBean.setUri("http://192.168.1.8:8082/jzgk/person/info");
        userActiveBeanList.add(userActiveBean);

        //第二条数据
//        userActiveBean = new UserActiveBean();
//        userActiveBean.setId(UUID.randomUUID().toString());
//        userActiveBean.setBizType("b2");
//        userActiveBean.setCardNo("cardNo2");
//        userActiveBean.setContent("七仙女的美好人生。" + _N + "和凡人过着普通而又幸福的日子");
//        userActiveBean.setDestIp("192.168.1.8");
//        userActiveBean.setDestPort(8082);
//        userActiveBean.setErrorCode(null);
//        userActiveBean.setErrorLog(null);
//        userActiveBean.setFormatParam("personId=2");
//        userActiveBean.setKeyWords("七仙女");
//        userActiveBean.setLogType("mysql");
//        userActiveBean.setModule("qi xian nv");
//        userActiveBean.setNetworkCode("3");
//        userActiveBean.setNetworkName("公安网");
//        userActiveBean.setParams("personId=2");
//        userActiveBean.setPoliceId("police2");
//        userActiveBean.setRequestId(UUID.randomUUID().toString());
//        userActiveBean.setResponse("ok");
//        userActiveBean.setResponseTime(70);
//        userActiveBean.setResponseType("200");
//        userActiveBean.setSessionId(UUID.randomUUID().toString());
//        userActiveBean.setSn("sn5678");
//        userActiveBean.setSource("12.108.5.7");
//        userActiveBean.setSourcePort(8043);
//        userActiveBean.setSystemId("jzgk");
//        userActiveBean.setSystemName("集中管控");
//        userActiveBean.setTerminalIp("10.5.6.3");
//        userActiveBean.setTime(System.currentTimeMillis() - 23 * 3500 );
//        userActiveBean.setUri("/girl/info");
//        userActiveBean.setUri("http://192.168.1.8:8082/jzgk/girl/info");
//        userActiveBeanList.add(userActiveBean);
        return userActiveBeanList;
    }
}
